
import { native, sys } from "cc";
import { DEBUG } from "cc/env";
import { AndroidInterface } from "myGlobalModule";

/**
 *  这里存放全局数据 和一些全局用到的方法
 */
export default class GameLogic {

	private static _runDevEnv: boolean = null // true 测试,false 正式
	private static _isAndroid: number | boolean = null

	private static _playerToken: string = "" //玩家token

	public static initGameLogic() {
		this.getIsDebugEnv()
		let curPlatform = this.getIsAndroid()
		if (curPlatform) {
			//注册安卓端调用游戏的方法
			if (!window.android) {
				window.android = {} as AndroidInterface
			}
		}
	}

	public static setPlayerToken(tStr: string) {
		this._playerToken = tStr
	}

	//获取玩家token
	public static getPlayerToken(): string {
		if (!this._playerToken) {
			this._playerToken = "server_token=s%3AO-yp4aEmZNUJcqkNw2M5BzFWmOKCmhWn.C2kFVcnNODwBaYZ3kK90B6gPwewjMIxzrYqEGSZWBPY";
		}
		return this._playerToken
	}

	//获取网络连接的地址
	public static getSocketIOUrl(): string {
		let devUrl = "https://dev-game.lovechatbar.com"
		let relUrl = "https://game.lovechatbar.com"
		if (this.getIsDebugEnv()) {
			return devUrl
		}
		return relUrl
	}

	/**
	 * 获取当前进入的是否是测试环境
	 */
	private static getIsDebugEnv() {
		if (this._runDevEnv !== null) {
			return this._runDevEnv
		}
		if (DEBUG) {
			this._runDevEnv = true
			return true
		}
		if (sys.os == sys.OS.ANDROID && sys.isNative) {
			let result: boolean = native.reflection.callStaticMethod("com/cocos/game/reflectInteraction", "getIsDev", "()Z");
			this._runDevEnv = result
			return result
		}
		return false
	}

	/**
	 * 获取当前的运行平台
	 * 0电脑 true安卓 false苹果 
	 * @returns 
	 */
	private static getIsAndroid() {
		if (this._isAndroid !== null) {
			return this._isAndroid
		}
		let temp: number | boolean = 0
		if (DEBUG) {
			temp = 0
		}
		if (sys.os == sys.OS.ANDROID) {
			temp = true
		} else if (sys.os == sys.OS.IOS) {
			temp = false
		}
		this._isAndroid = temp
		return temp
	}
}
